const {
  IntegralShopModel
} = require('../../../utils/integralshop-model.js');
const {
  MemberModel
} = require('../../../utils/member-model.js');
var utils =
  require("../../../utils/util.js");
var integralShopModel = new IntegralShopModel();
var memberModel = new MemberModel();
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    src: 'https://oss.kiscloud.net/wechat/images/no_phone.png',
    memberName: "", //会员名称
    memberImg: "", //会员头像
    leverCode: "", //会员等级code
    leverName: "", //等级名称
    integral: 0, //积分
    gameMenuList: [], //小游戏列表
    hotList: [], //hot专区
    integralList: [], //积分兑换礼
    gradeList: [], //V1-V9等级进阶礼
    placeholderFigure1: "https://oss.kiscloud.net/wechat/images/icon_no_photo_1.png", //占位图
    placeholderFigure2: "https://oss.kiscloud.net/wechat/images/icon_no_photo_2.png", //占位图
    isMoreShow: false,
    lowestLevel: "", //最低等级
    highestLevel: "", //最高等级
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getGameList(); //游戏菜单列表
    this.getItemAppletQuery(); //商品列表
    this.getAppletLevelScale(); //小程序商户最低和最高等级
  },

  /**
   *微信会员信息 
   */
  getUserInfo: function () {
    if (app.globalData.isLogin) {
      memberModel.userInfo({
        merchantId: wx.getStorageSync('merchantId')
      }).then(data => {
        var that = this;
        that.setData({
          memberName: data.nickname || "", //会员名称
          src: data.avatarUrl || "https://oss.kiscloud.net/wechat/images/no_phone.png", //会员头像
          leverCode: data.memberLevel ? 'V' + data.memberLevel : "V0", //会员等级
          leverName: data.memberLevelName ? data.memberLevelName : "", //等级名称
        });
      }).catch(err => {
        wx.showToast({
          title: `${err.error}`,
          icon: 'none',
          duration: 2000
        })
      })
    } else {
      this.setData({
        memberName: '注册/登录',
        src: "https://oss.kiscloud.net/wechat/images/no_phone.png",
        leverCode: "V0",
        leverName: ""
      });
    }
  },
  /**
   * 会员资产汇总
   */
  getUserAssetSummary: function () {
    if (app.globalData.isLogin) {
      memberModel.userAssetSummary({
        merchantId: wx.getStorageSync('merchantId')
      }).then(data => {
        let integralTotal = utils.formatCurrency(data.integralTotal) || 0;
        var that = this;
        that.setData({
          integral: integralTotal,
        });
      }).catch(err => {
        wx.showToast({
          title: `${err.error}`,
          icon: 'none',
          duration: 2000
        })
      })
    } else {
      this.setData({
        integral: ''
      });
    }
  },

  /**
   * 游戏菜单列表
   */
  getGameList: function () {
    var param = {
      merchantId: wx.getStorageSync('merchantId')
    }
    integralShopModel.gameList(param).then(data => {
      var that = this;
      that.setData({
        gameMenuList: data
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 商品列表
   */
  getItemAppletQuery: function () {
    var param = {
      merchantId: wx.getStorageSync('merchantId')
    }
    integralShopModel.itemAppletQuery(param).then(data => {
      var that = this;
      //moduleType 0 hot专区 1 积分兑好礼 2 等级进阶礼
      that.setData({
        hotList: data.filter(item => item.moduleType == 0) || [], //hot专区
        integralList: data.filter(item => item.moduleType == 1) || [], //积分兑换礼
        gradeList: data.filter(item => item.moduleType == 2) || [], //V1-V9等级进阶礼
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 小程序商户最低和最高等级
   */
  getAppletLevelScale: function () {
    var param = {
      merchantId: wx.getStorageSync('merchantId')
    }
    integralShopModel.appletLevelScale(param).then(data => {
      var that = this;
      that.setData({
        lowestLevel: data.lowestLevel ? "V" + data.lowestLevel : "V0", //最低等级
        highestLevel: data.highestLevel ? "-V" + data.highestLevel : "", //最高等级
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 跳转到小游戏页面
   */
  goGameHtml(event) {
    var activityName = event.currentTarget.dataset.activity;
    if (activityName && activityName != "" && activityName != null) {
      var gameConfigName = event.currentTarget.dataset.name; //游戏名称
      var activityType = event.currentTarget.dataset.type; //游戏类型
      var activityId = event.currentTarget.dataset.id;
      // var htmlUrl = event.currentTarget.dataset.url + '?gameConfigId=' + gameConfigId; //页面类型
      var htmlUrl = htmlUrl = `${event.currentTarget.dataset.url}?gameConfigName=${gameConfigName}&activityType=${activityType}&id=${activityId}`
      wx.navigateTo({
        url: htmlUrl
      })
    } else {
      wx.showToast({
        title: `敬请期待！`,
        icon: 'none',
        duration: 2000
      })
    }
  },

  /**
   * 跳转到兑换页面
   */
  goIntegralExchange(event) {
    this.getSubscribeMessage()
    var id = event.currentTarget.dataset.id; //商城布局id
    wx.navigateTo({
      url: '/pages/integralShop/integralExchange/index?mallLayoutId=' + id
    })
  },

  /**
   * 1.点击【我知道了】显示订阅消息弹窗
   * 2.点击【积分商城】进入积分详情页面时
   * 3.会员授权成功后
   * 4.点击【会员中心】按钮
   */
  getSubscribeMessage: function () {
    memberModel.subscribeMessage({
      merchantId: wx.getStorageSync('merchantId'),
      position: 2
    }).then(data => {
      if (data && data.length > 0) {
        let tmplId = []
        data.forEach(element => {
          if (element.templateId) {
            tmplId.push(element.templateId)
          }
        })
        if (tmplId.length > 0) {
          wx.requestSubscribeMessage({
            tmplIds: tmplId,
            success(res) {},
            fail(err) {}
          })
        }
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  //跳转到积分
  goIntegral: function () {
    if (app.globalData.isLogin) {
      wx.navigateTo({
        url: '/pages/memberCenter/integral/index',
      })
    } else {
      wx.navigateTo({
        url: '/pages/login/index'
      })
    }
  },
  /**
   * 显示更多数据
   */
  moreContent: function () {
    this.setData({
      isMoreShow: true
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      isLogin: app.globalData.isLogin
    })
    this.getUserInfo(); //会员信息
    this.getUserAssetSummary(); //积分
  }
})